export class pet {
    static tires = ["COMMON","UNCOMMON","RARE","EPIC","LEGENDARY","MYTHIC"]

    static expToNextLevel = [
        [100,110,120,130,145,160,175,190,210,230,250,275,300,330,360,400,440,490,540,600,660,730,800,880,960,1050,1150,1260,1380,1510,1650,1800,1960,2130,2310,2500,2700,2920,3160,3420,3700,4000,4350,4750,5200,5700,6300,7000,7800,8700,9700,10800,12000,13300,14700,16200,17800,19500,21300,23200,25200,27400,29800,32400,35200,38200,41400,44800,48400,52200,56200,60400,64800,69400,74200,79200,84700,90700,97200,104200,111700,119700,128200,137200,146700,156700,167700,179700,192700,206700,221700,237700,254700,272700,291700,311700,333700,357700,383700],
        [175,190,210,230,250,275,300,330,360,400,440,490,540,600,660,730,800,880,960,1050,1150,1260,1380,1510,1650,1800,1960,2130,2310,2500,2700,2920,3160,3420,3700,4000,4350,4750,5200,5700,6300,7000,7800,8700,9700,10800,12000,13300,14700,16200,17800,19500,21300,23200,25200,27400,29800,32400,35200,38200,41400,44800,48400,52200,56200,60400,64800,69400,74200,79200,84700,90700,97200,104200,111700,119700,128200,137200,146700,156700,167700,179700,192700,206700,221700,237700,254700,272700,291700,311700,333700,357700,383700,411700,441700,476700,516700,561700,611700],
        [275,300,330,360,400,440,490,540,600,660,730,800,880,960,1050,1150,1260,1380,1510,1650,1800,1960,2130,2310,2500,2700,2920,3160,3420,3700,4000,4350,4750,5200,5700,6300,7000,7800,8700,9700,10800,12000,13300,14700,16200,17800,19500,21300,23200,25200,27400,29800,32400,35200,38200,41400,44800,48400,52200,56200,60400,64800,69400,74200,79200,84700,90700,97200,104200,111700,119700,128200,137200,146700,156700,167700,179700,192700,206700,221700,237700,254700,272700,291700,311700,333700,357700,383700,411700,441700,476700,516700,561700,611700,666700,726700,791700,861700,936700,],
        [440,490,540,600,660,730,800,880,960,1050,1150,1260,1380,1510,1650,1800,1960,2130,2310,2500,2700,2920,3160,3420,3700,4000,4350,4750,5200,5700,6300,7000,7800,8700,9700,10800,12000,13300,14700,16200,17800,19500,21300,23200,25200,27400,29800,32400,35200,38200,41400,44800,48400,52200,56200,60400,64800,69400,74200,79200,84700,90700,97200,104200,111700,119700,128200,137200,146700,156700,167700,179700,192700,206700,221700,237700,254700,272700,291700,311700,333700,357700,383700,411700,441700,476700,516700,561700,611700,666700,726700,791700,861700,936700,1016700,1101700,1191700,1286700,1386700,],
        [660,730,800,880,960,1050,1150,1260,1380,1510,1650,1800,1960,2130,2310,2500,2700,2920,3160,3420,3700,4000,4350,4750,5200,5700,6300,7000,7800,8700,9700,10800,12000,13300,14700,16200,17800,19500,21300,23200,25200,27400,29800,32400,35200,38200,41400,44800,48400,52200,56200,60400,64800,69400,74200,79200,84700,90700,97200,104200,111700,119700,128200,137200,146700,156700,167700,179700,192700,206700,221700,237700,254700,272700,291700,311700,333700,357700,383700,411700,441700,476700,516700,561700,611700,666700,726700,791700,861700,936700,1016700,1101700,1191700,1286700,1386700,1496700,1616700,1746700,1886700,]
    ]

    static petItems = {
        "PET_ITEM_MINING_SKILL_BOOST_COMMON" : "§fMining Exp Boost", "MINING_SKILL_BOOST_UNCOMMON" : "§f§f§aMining Exp Boost", "PET_ITEM_MINING_SKILL_BOOST_RARE" : "§9Mining Exp Boost", "PET_ITEM_MINING_SKILL_BOOST_EPIC" : "§5Mining Exp Boost",
        "PET_ITEM_FARMING_SKILL_BOOST_COMMON" : "§fFarming Exp Boost","PET_ITEM_FARMING_SKILL_BOOST_UNCOMMON" : "§f§f§aFarming Exp Boost", "PET_ITEM_FARMING_SKILL_BOOST_RARE" : "§9Farming Exp Boost", "PET_ITEM_FARMING_SKILL_BOOST_EPIC" : "§5Farming Exp Boost",
        "PET_ITEM_FISHING_SKILL_BOOST_COMMON" : "§fFishing Exp Boost", "PET_ITEM_FISHING_SKILL_BOOST_UNCOMMON" : "§f§f§aFishing Exp Boost", "PET_ITEM_FISHING_SKILL_BOOST_RARE" : "§9Fishing Exp Boost", "PET_ITEM_FISHING_SKILL_BOOST_EPIC" : "§5Fishing Exp Boost",
        "PET_ITEM_COMBAT_SKILL_BOOST_COMMON" : "§fCombat Exp Boost", "PET_ITEM_COMBAT_SKILL_BOOST_UNCOMMON" : "§f§f§aCombat Exp Boost", "PET_ITEM_COMBAT_SKILL_BOOST_RARE" : "§9Combat Exp Boost", "PET_ITEM_COMBAT_SKILL_BOOST_EPIC" : "§5Combat Exp Boost",
        "PET_ITEM_FORAGING_SKILL_BOOST_COMMON" : "§fForaging Exp Boost", "PET_ITEM_FORAGING_SKILL_BOOST_UNCOMMON" : "§f§f§aForaging Exp Boost", "PET_ITEM_FORAGING_SKILL_BOOST_RARE" : "§9Foraging Exp Boost", "PET_ITEM_FORAGING_SKILL_BOOST_EPIC" : "§5Foraging Exp Boost",
        "PET_ITEM_ALL_SKILLS_BOOST_COMMON" : "§fAll Skills Exp Boost", "ALL_SKILLS_SUPER_BOOST" : "§f§f§fAll Skills Exp Super-Boost",

        "PET_ITEM_BIG_TEETH_COMMON" : "§fBig Teeth", "BIGGER_TEETH" : "§f§f§aBigger Teeth",
        "PET_ITEM_IRON_CLAWS_COMMON" : "§fIron Claws", "GOLD_CLAWS" : "§f§f§aGold Claws",
        "PET_ITEM_HARDENED_SCALES_UNCOMMON" : "§aHardened Scales", "REINFORCED_SCALES" : "§f§f§9Reinforced Scales",
        "PET_ITEM_SHARPENED_CLAWS_UNCOMMON" : "§aSharpened Claws", "SERRATED_CLAWS" : "§9Serrated Claws",
        "PET_ITEM_BUBBLEGUM" : "§9Bubblegum", "PET_ITEM_EXP_SHARE" : "§f§f§5Exp Share", "PET_ITEM_TIER_BOOST" : "§f§f§6Tier Boost",
        "PET_ITEM_SPOOKY_CUPCAKE" : "§aSpooky Cupcake", "PET_ITEM_TEXTBOOK" : "§f§f§6Textbook", "REAPER_GEM" : "§6Reaper Gem", "PET_ITEM_LUCKY_CLOVER" : "§f§f§5Lucky Clover",
        "BEJEWELED_COLLAR" : "§f§f§9Bejeweled Collar", "PET_ITEM_QUICK_CLAW" : "§f§f§5Quick Claw", "PET_ITEM_TITANIUM_MINECART" : "§9Titanium Minecart",
        "YELLOW_BANDANA" : "§f§f§9Yellow Bandana", "GREEN_BANDANA" : "§f§f§5Green Bandana", "BROWN_BANDANA" : "§9Brown Bandana",
        "ANTIQUE_REMEDIES" : "§5Antique Remedies", "CROCHET_TIGER_PLUSHIE" : "§5Crochet Tiger Plushie", "DWARF_TURTLE_SHELMET" : "§9Dwarf Turtle Shelmet", "WASHED_UP_SOUVENIR" : "§f§f§6Washed-up Souvenir", "MINOS_RELIC" : "§f§f§5Minos Relic",
        "PET_ITEM_SADDLE" : "§f§f§aSaddle", "PET_ITEM_FLYING_PIG" : "§aFlying Pig", "FOUR_EYED_FISH" : "§f§f§5Four-Eyed Fish", "UNCOMMON_PARTY_HAT" : "§aUncommon Party Hat"
    }
    
    /**
     * 宠物信息的Json文本
     * @param {string} petInfo 
     */
    constructor(petInfo) {
        let info = typeof petInfo == "string" ? JSON.parse(petInfo) : petInfo;
        this.type = info.type;
        this.exp = parseInt(info.exp);
        this.tier = isNaN(info.tier) ? pet.tires.indexOf(info.tier) : parseInt(info.tier);
        this.heldItem = info.heldItem ? info.heldItem : null;
        this.candyUsed = info.candyUsed;
        this.uuid = info.uuid;
        this.uniqueId = info.uniqueId;
    }

    getName() { return this.type; }
    getExp() { return this.exp; }
    getTier() { return this.tier; }
    getHeldItemId() { return this.heldItem; }
    getHeldItemName() { 
        if(!this.heldItem) return null;
        return pet.petItems[this.heldItem] ?? this.heldItem.replace("PET_ITEM_","").replace(/_/g," ").toLowerCase();
    }
    getHeldItem() {return this.getHeldItemName(); }
    getCandyUsed() { return this.candyUsed; }
    getUUID() { return this.uuid; }
    getUniqueId() { return this.uniqueId; }
    getLevel() {
        let xp = this.exp;
        let level = 1;
        let tier = this.tier + (this.heldItem && this.heldItem == "PET_ITEM_TIER_BOOST" ? 1 : 0);
        for (let xpToNextLevel of pet.expToNextLevel[tier > 4 ? 4 : tier]) {
            if (xp < xpToNextLevel) break;
            xp -= xpToNextLevel;
            level++;
        }
        if(this.type === "GOLDEN_DRAGON" && level >= 100) {
            level++;
            if(xp >= 5555) {
                level++;
                xp -= 5555;
                for (let xpToNextLevel = 1886700; level < 200; level++) {
                    if(xp < xpToNextLevel) break;
                    xp -= xpToNextLevel;
                }
            }
        }
        return level;
    }
}